<template>
  <div>
    <div class="container">
      <!-- 卡片视图区域 -->
      <el-card>
        <h3>专业列表</h3>
        <el-table :data="subjects" style="width: 100%" border>
          <el-table-column prop="_id" label="id" width="180"> </el-table-column>
          <!-- 姓名 -->
          <el-table-column prop="name" label="学科名称" width="180">
            <!-- <template width="90" slot-scope="scope">
          <el-input
            type="text"
            v-model="changeStudent.name"
            v-show="scope.row.isEdit"
          />
          <span v-show="!scope.row.isEdit">{{ scope.row.name }}</span>
        </template> -->
          </el-table-column>

          <!-- 操作 -->
          <el-table-column label="操作" width="200">
            <template slot-scope="scope">
              <el-button size="mini" type="danger" icon="el-icon-delete"
                @click="deleteSubject(scope.row._id)">删除</el-button>

              <!-- <el-button size="mini" type="primary" v-show="!scope.row.isEdit"
              icon="el-icon-edit" @click="change(scope.row._id)">
              修改
            </el-button>

            <el-button size="mini" type="success" v-show="scope.row.isEdit"
              @click="upChange"><i class="el-icon-upload el-icon--right"></i>保存
            </el-button> -->
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="pages">
          <el-pagination @size-change="changeSize"
            @current-change="currentChange"
            :current-page="queryInfo.currentPage" :page-sizes="[1, 2, 5, 10]"
            :page-size="queryInfo.pageSize"
            layout="total, sizes, prev, pager, next, jumper" :total="total">
          </el-pagination>
        </div>
      </el-card>
    </div>

  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapState, mapActions } = createNamespacedHelpers("subject");
export default {
  data() {
    return {
      queryInfo: { pageSize: 5, currentPage: 1 },
    }
  },
  computed: {
    ...mapState(['subjects', 'total'])
  },
  methods: {
    ...mapActions(['getSubject']),
    // 每一页展示数量
    changeSize(i) {
      this.queryInfo.pageSize = i;
    },
    // 页数
    currentChange(i) {
      this.queryInfo.currentPage = i;
    },
    deleteSubject(_id) {
      this.$api.subjectApi.deleteSubject(_id).then(({ data }) => {
        this.$message.success(data)
        this.getSubject({ params: this.queryInfo })
      })
    }
  },
  created() {
    this.getSubject({ params: this.queryInfo })
  },
  watch: {
    queryInfo: {
      handler() {
        this.getSubject({ params: this.queryInfo })
      },
      deep: true
    }
  },
};
</script>

<style scoped>
.container {
  width: 610px;
}
</style>